package org.lanqiao.tjut.controller;


import java.io.IOException;


import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.lanqiao.tjut.bean.TBStudentBean;
import org.lanqiao.tjut.model.ModelFactory;
import org.lanqiao.tjut.myUtils.MyUtils;

/**
 * Servlet implementation class StudentInfoAdd
 */
@WebServlet("/StudentInfoAddServlet")
@MultipartConfig(maxFileSize=5000000)
public class StudentInfoAddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		TBStudentBean stuB=MyUtils.convertRequest2Bean(request, TBStudentBean.class, 
				"uploadfies/stuImgs/", "stImg");
		System.out.println("stu::"+stuB.toString());
		
		
		/*TBStudentBean stuB=MyUtils.getNewInstance(TBStudentBean.class);
		stuB.setStudent_name(request.getParameter("stuName"));
		stuB.setSex(request.getParameter("stuSex"));
		stuB.setBirthday(MyUtils.convertString2Date(request.getParameter("stuBirthday"), "yyyy-MM-dd hh:mm:ss"));
		stuB.setAddress_id(request.getParameter("stuAddress"));
		stuB.setStu_tel(request.getParameter("stuTel"));
		Part part=request.getPart("stuImg");
		String strFileOldName=part.getSubmittedFileName();
		String relPath=request.getServletContext().getRealPath("/");
		String uploadFilePath="uploadfiles/stuImgs/";
		File f=new File(relPath+uploadFilePath);
		if(!f.exists()){
			f.mkdirs();
		}
		System.out.println("打印上传文件目录"+f.getAbsolutePath());
		
		String strFileType=strFileOldName.substring(strFileOldName.lastIndexOf('.'));
		String strFilePrefix="stuImg";
		
		String strUploadFileName=strFilePrefix+UUID.randomUUID()+strFileType;
		part.write(relPath+uploadFilePath+strUploadFileName);
		stuB.setStu_img(uploadFilePath+strUploadFileName);
		// 输出打印前台表单数据
				System.out.println("stu:"+stuB.toString());
				// 调用model层进行数据库保存*/
				int i = ModelFactory.getStudentInfoModelInstance().saveOrUpdateStudentInfo(stuB);
				// 保存之后的跳转处理
				if (i > 0) {
					// 保存成功
					request.getRequestDispatcher("WEB-INF/logined/studentInfoManager/studentInfoAddDetail.jsp").forward(request,
							response);
				} else {
					// 保存失败
					response.getWriter().write(
							"<script language='javascript'>alert('数据保存失败!');"
							+ "location.href='DispatchCommServlet?"
							+ "jp=WEB-INF/logined/studentInfoManager/studentInfoAdd';</script>");

				}
		
	}

}
